//
//  BaseImgTitleTextVC.m
//  RuiBeiKang
//
//  Created by 江伟 on 2018/11/29.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "BaseImgTitleTextVC.h"
@interface BaseImgTitleTextVC ()
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) BOOL isDataLoaded;/// 是否第一次加载
@property (nonatomic,copy) NSString *page;
@end

@implementation BaseImgTitleTextVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        _page = @"1";
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    /**
     由于多个页面已创建会接收网络加载完成通知显示暂无数据占位图
     需要手动设置为正在加载数据显示空白占位图
     */
    self.isLoading = YES;
    [self.tableView reloadEmptyDataSet];
    [self viewWillLoadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupRefresh];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupViews {
    [self.tableView registerNib:[UINib nibWithNibName:@"ImageTitleTextCell1" bundle:nil] forCellReuseIdentifier:@"ImageTitleTextCell1"];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

- (void)setupRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
        [self loadRequestPage:self.page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = SF(@"%ld",[self.page integerValue] + 1);
        [self loadRequestPage:self.page];
    }];
    /// 设置contentInset 补缺
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = HomeIndicator_Height;
}

- (void)endRefresh {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}

- (void)viewWillLoadData {
    if (!_isDataLoaded) {

        self.page = @"1";
        [self loadRequestPage:self.page];
        self.isDataLoaded = YES;
    }
}

- (void)loadRequestPage:(NSString *)page {
    [self itt_loadRequestPage:page finsihBlock:^(NSArray *modelArr, NSNumber *isNoMore) {
        if (modelArr) {
            NSLog(@"成功回调");
            
            if ([isNoMore integerValue] == 0) {
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self endRefresh];
            }
            if ([self.page integerValue] > 1) {
                [self.dataArr addObjectsFromArray:modelArr];
            } else {
                self.dataArr = [NSMutableArray arrayWithArray:modelArr];
            }
            [self.tableView reloadData];
        } else {
            NSLog(@"失败回调");
            [self endRefresh];
        }
    }];
}

- (void)itt_loadRequestPage:(NSString *)page finsihBlock:(ImgTitleTextVCNetworkBlock)block {
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ImageTitleTextCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"ImageTitleTextCell1"];
    cell.model = self.dataArr[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return aufont(90);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ImageTitlteTextModel *model = self.dataArr[indexPath.section];
    [self itt_tableView:tableView didSelectRowAtIndexPath:indexPath model:model];
}

- (void)itt_tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath model:(ImageTitlteTextModel *)model {
    
}

/// 重写
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self viewWillLoadData];
    [scrollView reloadEmptyDataSet];
    NSLog(@"点击试图刷新");
}

- (NSMutableArray *)dataArr {
    if(!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
